<?php
// Init owncloud

require_once(OC_App::getAppPath('jobposition') . "/lib/position.php");

OCP\JSON::callCheck();
if(!OC_User::isLoggedIn()) {
   echo '您需要先登录';
   exit;
} else {
   if (PFactory::isJobseekerUser()) {
      echo "您无权访问该资源！";
      exit;
   }
}

$question_id = JRequest::getInt('qid', 0);
$job_id = JRequest::getInt('job_id', 0);
$sheet_id = JRequest::getInt('sheet_id', 0);
$is_new = JRequest::getInt('is_new', 0);

$model = new PJobpositionModelPosition();
$q = $model->getQuestionDetail($question_id, $sheet_id);

if(empty($q)) {
   echo '考题似乎丢失了';
   exit;
}
$ques_key = array('0'=>'A','1'=>'B','2'=>'C','3'=>'D','4'=>'E');
?>
<?php if (empty($is_new)) : ?>
<div class="adp_collapseastatus clearfix">
   <span>[<?=$q->typename?>]</span>
   <a href="javascript:;" class="papertesttitle"><?=$q->question?></a>
   <em>(<?=$q->score?>分)</em>
   <?php if ($q->hits == 0) : ?>
   <a href="javascript:;" class="papertestdelete pull-right"></a>
   <?php endif;?>
   <a href="javascript:;" class="papertestedit pull-right"></a>
</div>
<?php else : ?>
<div class="each_ques_block" data="{qid:'<?=$q->questionid?>',job_id:'<?=$job_id?>',sheet_id:'<?=$q->sheetid?>'}">
   <div id="each_ques_info_<?=$q->questionid?>" class="adp_borderbox adp_borderbox_blurbg eachquestion">
      <div class="adp_collapseastatus clearfix">
         <span>[<?=$q->typename?>]</span>
         <a href="javascript:;" class="papertesttitle"><?=$q->question?></a>
         <em>(<?=$q->score?>分)</em>
         <?php if ($q->hits == 0) : ?>
         <a href="javascript:;" class="papertestdelete pull-right"></a>
         <?php endif;?>
         <a href="javascript:;" class="papertestedit pull-right"></a>
      </div>
   </div>
<!-- Hidden each question action form -->
<form action="<?php echo OC_Helper::linkToRoute("jobpos_savequestion");?>" method="post" name="each_ques_form_<?=$q->questionid?>" id="each_ques_form_<?=$q->questionid?>" style="display:none;" class="each_ques_form">
<div class="adp_borderbox adp_borderbox_blurbg">
   <div class="form-horizontal adp-form-horizontal">
      <?php if ($q->hits == 0) : ?>
      <div class="control-group">
         <label class="control-label"><em class="redstar">*</em> 分值：</label>
         <div class="controls">
            <input name="score" type="text" class="input-medium required digits" value="<?=$q->score?>">
         </div>
      </div>
      <?php else: ?>
      <div class="control-group">
         <label class="control-label"> 分值：</label>
         <div class="controls">
            <span><?=$q->score?>分</span>&nbsp;<span class="adp_yellowtip">该题已有应聘者做过, 不可修改题目分值</span>
         </div>
      </div>
      <input name="score" type="hidden" value="<?=$q->score?>">
      <?php endif;?>
      <div class="control-group">
         <label class="control-label"><em class="redstar">*</em> 题目：</label>
         <div class="controls">
            <textarea name="question" class="required" style="width:500px; height:100px;"><?=$q->question?></textarea>
         </div>
      </div>
      <?php if ($q->type == 'freeinput') : ?>
      <div class="control-group">
         <label class="control-label">参考答案：</label>
         <div class="controls">
            <textarea name="answer" style="width:500px; height:100px;"><?php echo !empty($q->answer) ? trim($q->answer[0]) : '';?></textarea>
         </div>
      </div>
      <?php elseif ($q->type == 'singleanswer') :?>
      <div class="control-group">
         <label class="control-label"><em class="redstar">*</em> 选项：</label>
         <div class="controls">
            <?php for ($i=0; $i < 5; $i++) { ?>
               <div class="adpeachanswer">
               <input type="text" name="singleanswer[]" value="<?php echo !empty($q->answer[$i]) ? $q->answer[$i]->choice : '';?>" style="width:400px;" class="<?php echo (!empty($q->answer[$i]) && $q->answer[$i]->answer == 1) ? "must_fill" : ''?>">
               <span class="adppostion_text"><?=$ques_key[$i]?></span>
               <label class="radio inline">
                  <input type="radio" name="correctanswer" class="{required:true,messages:{required:'请选择正确答案！'}}" value="<?=$i?>" <?php echo (!empty($q->answer[$i]) && $q->answer[$i]->answer == 1) ? "checked='checked'" : ''?>/>
                  <span class="graycolor">正确答案</span>
               </label>
            </div>
            <?php }?>
         </div>
      </div>
      <?php endif;?>
      <div class="control-group">
         <div class="controls">
            <button class="btn submit_btn" disabled="disabled">保存题目</button>
            <a href="javascript:;" class="medium adpresetbtn">取消</a>
            <span id='errormsg' class='error' style='color:red;display:none;'></span>
            <input type="hidden" name="typeid" value="<?=$q->typeid?>"/>
            <input type="hidden" name="job_id" value="<?=$job_id?>"/>
            <input type="hidden" name="sheet_id" value="<?=$q->sheetid?>"/>
            <input type="hidden" name="question_id" value="<?=$q->questionid?>"/>
            <input type="hidden" name="requesttoken" value="<?php echo PSession::getFormToken();?>" id="requesttoken">
         </div>
      </div>
   </div>
</div>
</form>
</div>
<?php endif;?>
